package com.applet.mallappletbackstage.controller;


import com.applet.mallappletbackstage.entity.ReleaseWork;
import com.applet.mallappletbackstage.entity.UserReleaseWork;
import com.applet.mallappletbackstage.service.ReleaseWorkService;
import com.applet.mallappletbackstage.service.UserReleaseWorkService;
import com.applet.mallappletbackstage.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

/**
 * 工作相关
 *
 * @author ${author}
 * @since 2020-07-31
 */
@RestController
@RequestMapping("/release-work")
public class ReleaseWorkController {

    @Autowired
    ReleaseWorkService releaseWorkService;
    @Autowired
    UserReleaseWorkService userReleaseWorkService;

    /**
     * 获取一个工作
     *
     * @param id
     * @return
     */
    @GetMapping("work/one/{id}")
    public R getReleaseWork(@PathVariable String id) {
        ReleaseWork work = releaseWorkService.getById(id);
        return R.success(work);
    }

    /**
     * 获取工作列表
     *
     * @param current
     * @return
     */
    @GetMapping("work/{current}")
    public R getReleaseWorkList(@PathVariable("current") Integer current,
                                @RequestParam(required = false) String typeOfWork,
                                @RequestParam(required = false) String title,
                                @RequestParam(required = false) String date) {
        Page<ReleaseWork> releaseWorkPage = new Page<>(current, 15);
        QueryWrapper<ReleaseWork> releaseWorkQueryWrapper = new QueryWrapper<>();
        if (title != null) {
            releaseWorkQueryWrapper.like("title", title);
        }
        if (typeOfWork != null) {
            releaseWorkQueryWrapper.eq("type_of_work", typeOfWork);
        }
        if (date != null) {
            releaseWorkQueryWrapper.ge("gmt_create", date);
        }
        releaseWorkQueryWrapper.eq("status", 1);
        Page<ReleaseWork> page = releaseWorkService.page(releaseWorkPage, releaseWorkQueryWrapper);
        return R.success(page);
    }

    /**
     * 发布工作
     *
     * @param releaseWork
     * @return
     */
    @PostMapping("work/{id}")
    public R saveReleaseWork(@RequestBody ReleaseWork releaseWork, @PathVariable("id") String id) {
        boolean save = releaseWorkService.save(releaseWork);
        UserReleaseWork userReleaseWork = new UserReleaseWork();
        userReleaseWork.setReleaseWorkId(releaseWork.getId()).setUserId(id);
        userReleaseWorkService.save(userReleaseWork);
        return R.success("成功");
    }

    /**
     * 修改工作
     *
     * @param releaseWork
     * @return
     */
    @PutMapping(value = "work")
    public R updateReleaseWork(@RequestBody ReleaseWork releaseWork) {
        releaseWorkService.updateById(releaseWork);
        return R.success();
    }

    @DeleteMapping("work/{releaseWorkId}")
    public R deleteReleaseWork(@PathVariable("releaseWorkId") String releaseWorkId) {
        boolean isFix = releaseWorkService.removeById(releaseWorkId);
        QueryWrapper<UserReleaseWork> userReleaseWorkQueryWrapper = new QueryWrapper<>();
        userReleaseWorkQueryWrapper.eq("release_work_id", userReleaseWorkQueryWrapper);
        userReleaseWorkService.remove(userReleaseWorkQueryWrapper);
        return R.success();
    }

    /**
     * 服务器获取数据 并且修改数据
     *
     * @param current
     * @param typeOfWork
     * @param title
     * @param date
     * @return
     */
    @GetMapping(value = "admin/work/{current}")
    public R adminGetReleaseWorkList(@PathVariable("current") Integer current,
                                     @RequestParam(required = false) String typeOfWork,
                                     @RequestParam(required = false) String title,
                                     @RequestParam(required = false) String date) {
        Page<ReleaseWork> releaseWorkPage = new Page<>(current, 10);
        QueryWrapper<ReleaseWork> releaseWorkQueryWrapper = new QueryWrapper<>();
        if (title != null) {
            releaseWorkQueryWrapper.like("title", title);
        }
        if (typeOfWork != null) {
            releaseWorkQueryWrapper.eq("type_of_work", typeOfWork);
        }
        if (typeOfWork != null) {
            releaseWorkQueryWrapper.ge("gmt_create", date);
        }
        Page<ReleaseWork> page = releaseWorkService.page(releaseWorkPage, releaseWorkQueryWrapper);
        return R.success().data("verify", page);
    }

    /**
     * 页面
     *
     * @param id
     * @return
     */
    @GetMapping("admin/edit/{id}")
    public ModelAndView editWork(@PathVariable String id) {
        ModelAndView modelAndView = new ModelAndView("edit");
        ReleaseWork work = releaseWorkService.getById(id);
        modelAndView.addObject("work", work);
        return modelAndView;
    }

    /**
     * 页面
     *
     * @return
     */
    @GetMapping("admin/work")
    public ModelAndView work() {
        ModelAndView modelAndView = new ModelAndView("work");
        return modelAndView;
    }

    /**
     * 页面
     *
     * @return
     */
    @PostMapping("upload/excel")
    public ModelAndView uploadExcel(MultipartFile multipartFile) {
        try {
            boolean isSuccess = releaseWorkService.uploadExcel(multipartFile);
            if (!isSuccess) return null;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ModelAndView("redirect:/release-work/admin/work");
    }

    /**
     * 页面
     */
    @GetMapping("admin/ucenter")
    public ModelAndView ucenter() {
        ModelAndView modelAndView = new ModelAndView("ucenter");
        return modelAndView;
    }

    /**
     * 获取数据
     *
     * @param id
     * @param status
     * @return
     */
    @GetMapping("work/mine/{id}")
    @ApiOperation("根据用户Id获取工作，0代表全部数据")
    public R getWorkByUserId(@PathVariable String id, @RequestParam int status) {
        List<ReleaseWork> releaseWorks = userReleaseWorkService.selectWorkById(id, status);
        return R.success().data(releaseWorks);
    }

    @GetMapping("search/{current}")
    public R searchCourse(@PathVariable Long current, String key) {
        Page<ReleaseWork> page = new Page<>(current, 15);
        QueryWrapper<ReleaseWork> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", key).or().like("claim", key).or().like("company", key);
        queryWrapper.and(releaseWorkQueryWrapper -> releaseWorkQueryWrapper.ne("status", "0"));
        Page<ReleaseWork> releaseWorkPage = releaseWorkService.page(page, queryWrapper);
        return R.success(releaseWorkPage);
    }
}
